package javabase.thinkinjava.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author fance
 * @date 2018/8/14 8:42
 */
public class Demo {
    private  int val;
    private  int big;

    public Demo(int val) {
        this.val = val;
    }

    public Demo(int val, int big) {
        this.val = val;
        this.big = big;
    }

    public Demo() {
    }

    @Override
    public String toString() {
        return "Demo{" +
                "val=" + val +
                ", big=" + big +
                '}';
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> demo = Class.forName("javabase.thinkinjava.reflect.Demo");
        Field[] fields = demo.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        Constructor constructor = demo.getConstructor(int.class);
        Demo demo1 = (Demo) constructor.newInstance(2);
        System.out.println(demo1);
        Method method = demo.getDeclaredMethod("toString");
        method.invoke(demo1);
    }
}
